package mobile.persistencia;

import waba.io.*;
import waba.util.Vector;
import waba.sys.*;
import mobile.dominio.*;
import moio.util.*;

public class PZona extends Catalog {
	private ResizeRecord rs;
	private DataStream ds;

	public static PZona instance = new PZona();

	/**
	 * Constructor which creates or open ( if it already exists ) the Customers.pdb file,
	 * attach the ResizeRecord and DataStream. The pragmatic way of reading/writing to a
	 * Catalog is by attaching those streams (ResizeRecord/DataStream) so that the resizing
	 * of the Catalog and information writing/reading are performed by those classes
	 */
	public PZona()
	{
		super("Zona.mGqv.DATA", Catalog.CREATE);
		//super("Zona."+ Settings.appCreatorId +".DATA", Catalog.CREATE);
		rs = new ResizeRecord(this, 128);
		ds = new DataStream(rs);
	}

	public void write(Zona unaZona, boolean update)
	{
		try {
			if (update)
			{
				int n = indexOf(unaZona);
				if (n == -1 || !rs.restartRecord(n)) 
					return;
			}
			else
				rs.startRecord(); /* start the record at the next available position */

			unaZona.saveTo(ds);
			rs.endRecord(); /* finishes the writing of a record */
		} catch (IOError e) {}
	}


	/**
	 * Removes a record ( Customer ) from the pdb file
	 * 
	 * @param cli
	 *           the Customer
	 */
	public void remove(Zona unaZona)
	{
		/*
		 * We must set the record position to the record we want to delete, and then delete
		 * the record.
		 */
		int n = indexOf(unaZona);
		try {
			if (n >= 0 && this.setRecordPos(n)){
				this.deleteRecord();
				return;
			}
		} catch (IOError e) {}
	}


	/**
	 * Find the index ( position ) of the record ( Customer ) inside the pdb file
	 * 
	 * @param cli
	 *           the Customer
	 * @return the index position or -1 if the Customer was not found on the record
	 */
	public int indexOf(Zona unaZona)
	{
		for (int n = this.getRecordCount() - 1; n >= 0; n--)
			try {
				if (this.setRecordPos(n)) {
					Zona laZona = read();

					if (laZona.getId() == unaZona.getId()) {
						return n;
					}
				}
			} catch (IOError e) {

			}
			return -1;
	}

	/**
	 * Reads a record ( Customer ) and construct a Customer. Notice that the order in which
	 * you read informations from the Customer is relevant.
	 * 
	 * @return the Customer
	 */
	public Zona read()
	{
		return new Zona(ds);
	}

	/**
	 * Returns a specific record ( Customer )
	 * 
	 * @param name
	 *           Name of the Customer
	 * @return The Customer or null if the Customer was not found
	 */
	public Zona obtenerZonaXID(int id){
		for (int n = this.getRecordCount() - 1; n >= 0; n--){
			try {
				if (this.setRecordPos(n)){
					Zona unaZona = read();
					if (id == unaZona.getId()){
						return unaZona;
					}
				}
			} catch(IOError e){
			}
		}
		return null;
	}
	
   public ArrayList todasLasZonas(){
	   ArrayList colZonas = new ArrayList();
		for (int n = this.getRecordCount() - 1; n >= 0; n--){
			try {
				if (this.setRecordPos(n)){
					Zona unaZona = read();
					colZonas.add(unaZona);
				}
			} catch(IOError e){
			}
		}
		return colZonas;
   }
}